﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using CQIE.TES.IService;
using CQIE.TES.Model;
using CQIE.TES.Service;
using Newtonsoft.Json.Linq;

namespace CQIE.TES.WEB.ADMIN.Controllers.Evaluating
{
    public class EvaluatingAPIController : ApiController
    {
        public IEvaluaHeightService HeightService { get; set; }

        /// <summary>
        /// 发布评教
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/saveevaluate")]
        public bool SaveEvaluate([FromBody] JObject data)
        {
            // 接受实例
            EvaluaHeight model = new EvaluaHeight()
            {
                Name = data.Value<String>("Title"),
                EndTime = data.Value<DateTime>("EndTime"),
                StartTime = data.Value<DateTime>("StartTime"),
                StudyYearId = data.Value<int>("StudyYear"),
                SHeight = data.Value<double>("StudentHeight"),
                LHeight = data.Value<double>("LeaderHeight"),
                THeight = data.Value<double>("TeacherHeight"),
                MHeight = data.Value<double>("SelfHeight"),
                DHeight = data.Value<double>("DepartmentHeight")
            };
            // 计算比重百分比
            double? total = model.SHeight + model.LHeight + model.THeight + model.MHeight + model.DHeight;
            model.SHeight = Math.Round((double)(model.SHeight / total), 2);
            model.LHeight = Math.Round((double)(model.LHeight / total), 2);
            model.THeight = Math.Round((double)(model.THeight / total), 2);
            model.MHeight = Math.Round((double)(model.MHeight / total), 2);
            model.DHeight = Math.Round((double)(model.DHeight / total), 2);

            bool IsSuccess = HeightService.AddEva(model);
            return IsSuccess;
        }

        /// <summary>
        /// 获取所有评教
        /// </summary>
        /// <returns></returns>
        [Route("api/eva/getall")]
        [HttpGet]
        public object GetAllEva(int limit,int page,int studyYearId=-1)
        {
            var list = HeightService.GetAllEva(page,limit,out int count,studyYearId);
            Func<DateTime, DateTime, string> getState = (startTime, endTime) =>
            {
                DateTime now = DateTime.Now;
                if (now < startTime)
                {
                    return "未开始";
                }

                if (now > startTime && now < endTime)
                {
                    return "进行中";
                }

                return "已完成";
            };

            var data =
                new
                {
                    code = 0,
                    msg = "success",
                    count,
                    data = list.Select(r => new
                    {
                        id = r.ID,
                        name = r.Name,
                        startTime = r.StartTime.Value.ToShortDateString(),
                        endTime = r.EndTime.Value.ToShortDateString(),
                        SHeight = (r.SHeight * 100) + "%",
                        DHeight = (r.DHeight * 100) + "%",
                        LHeight = (r.LHeight * 100) + "%",
                        THeight = (r.THeight * 100) + "%",
                        MHeight = (r.MHeight * 100) + "%",
                        state = getState((DateTime) r.StartTime,(DateTime) r.EndTime),
                        studyYear = r.StudyYear.Name,
                        studyYearId = r.StudyYearId
                    })
                };

            return data;
        }

        /// <summary>
        /// 立即开始
        /// </summary>
        /// <param name="res">res中有一个参数:eid为评教的id</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/eva/startnow")]
        public object StartNowEva([FromBody]JObject res)
        {
            int evaId = res.Value<int>("eid");
            bool isSuccess = HeightService.StartEvaNow(evaId);
            var data = new
            {
                msg = isSuccess ? "成功":"失败"
            };
            return data;
        }

        /// <summary>
        /// 立即结束
        /// </summary>
        /// <param name="res">res中有一个参数:eid为评教的id</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/eva/endnow")]
        public object EndNowEva([FromBody] JObject res)
        {
            int evaId = res.Value<int>("eid");
            bool isSuccess = HeightService.EndEvaNow(evaId);
            var data = new
            {
                msg = isSuccess ? "成功" : "失败"
            };
            return data;
        }


    }
}
